// pages/message/message.js
var animation;
var userInfo;
import WxValidate from '../../utils/WxValidate';
const request = require('../../utils/request.js');
const rules = {
  name: {
    required: true,
    rangelength: [2, 10]
  },
  idCard: {
    required: true,
    idcard: true,
  },
}
const messages = {
  name: {
    required: "请输入真实姓名",
    rangelength: "姓名为2-10个汉字"
  },
  idCard: {
    required: "请输入身份证号码",
    idcard: "请输入正确的身份证号码",
  }
}
Page({

  /**
   * 页面的初始数据
   */
  data: {
    screenHeight: "",
    userInfo: "",
    animationData: {},
    maskFlag: true,
    zhuhuFlag: false,
    isUser: "管理人员",
    isRegident: "", //小区住户
    status: 2, //审核状态
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    // 显示本地的屏幕高度
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          screenHeight: res.screenHeight - 44 - res.statusBarHeight
        })
      },
    })
    this.WxValidate = new WxValidate(rules, messages);
    animation = wx.createAnimation({
      duration: 1000,
      timingFunction: 'ease',
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    userInfo = wx.getStorageSync('userInfo');
    var p;
    var zhuhu;
    if (userInfo.isProsecutor != "") {
      if (userInfo.isProsecutor === 0) {
        p = "用户"
      } else {
        p = "检查人员"
      }
    } else {
      p = "用户"
    }
    if (userInfo.isRegident != "") {
      if (userInfo.isRegident === 0) {
        zhuhu = 0
      } else {
        zhuhu = 1
      }
    } else {
      zhuhu = 0;
    }
    this.setData({
      userInfo: userInfo,
      isUser: p,
      isRegident: zhuhu,
    })
    request.getExamine(userInfo.id, this.data.userInfo.token).then(res => {
      console.log(res);
      if (res.data.code == 200) {
        that.setData({
          status: res.data.data.status
        })
        console.log(res.data)
        wx.setStorageSync('status', res.data.data.status)
      }
      if(res.data.data.status==2){
        wx.showToast({
          title: '您的审核不通过，请重新审核',
          icon:'none',
          duration:3000,
        })
      }
    })
  },
  radioChange: function (e) {
    console.log(e);
    if (e.detail.value == 0) {
      this.setData({
        zhuhuFlag: false,
      })
      this.data.userInfo.isRegident = false;
    } else {
      this.setData({
        zhuhuFlag: true
      })
      this.data.userInfo.isRegident = true;
    }

  },
  edit: function () {
    this.setData({
      maskFlag: false
    })
    animation.translateY(-375).step()
    this.setData({
      animationData: animation.export()
    })
  },
  close: function () {
    animation.translateY(375).step()
    this.setData({
      animationData: animation.export(),
      maskFlag: true
    })
  },
  sure: function (e) {
    var that = this;
    console.log(this.data.userInfo);
    if (!this.WxValidate.checkForm(e.detail.value)) {
      console.log(this.WxValidate)
      const error = this.WxValidate.errorList[0]
      wx.showModal({
        content: error.msg,
        showCancel: false,
      })
    } else {
      var data;
      if (this.data.userInfo.isRegident == true) {
        data = {
          // id:this.data.userInfo.id,
          wxUserId: this.data.userInfo.id,
          name: this.data.userInfo.name,
          idCard: this.data.userInfo.idCard,
          isRegident: this.data.userInfo.isRegident,
          roomNumber: this.data.userInfo.roomNumber,
        }
      } else {
        data = {
          wxUserId: this.data.userInfo.id,
          name: this.data.userInfo.name,
          idCard: this.data.userInfo.idCard,
          isRegident: this.data.userInfo.isRegident,
          roomNumber: "",
        }
      }
      // request.user(data,this.data.userInfo.token).then(res=>{
      //   if(res.data.code==200){
      //     console.log("更新成功")
      //     wx.showToast({
      //       title: '更新个人信息成功',
      //       icon:'none'
      //     })
      //     wx.setStorageSync('userInfo', that.data.userInfo);
      //     that.setData({
      //       maskFlag:true,
      //       userInfo:that.data.userInfo,
      //       isRegident:that.data.userInfo.isRegident
      //     })
      //   }
      // })
      request.examine(data, this.data.userInfo.token).then(res => {
        if (res.data.code == 200) {
          console.log("提交成功");
          console.log(res);
          that.setData({
            status: 0,
          })
          wx.showToast({
            title: '提交审核成功',
            icon: 'none',
            duration: 2000,
          })
          console.log(that.data.userInfo);
          wx.setStorageSync('userInfo', that.data.userInfo);
          wx.setStorageSync('status', 0);
          that.setData({
            maskFlag: true,
            userInfo: that.data.userInfo,
            isRegident: that.data.userInfo.isRegident
          })
        }
      })
    }
  },
  bindNameChange: function (e) {
    console.log(e);
    this.data.userInfo.name = e.detail.value;
  },
  bindIdChange: function (e) {
    this.data.userInfo.idCard = e.detail.value;
  },
  bindRoomChange: function (e) {
    this.data.userInfo.roomNumber = e.detail.value;
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },


  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})